//
//  ZXHeaderCollectionReusableView.swift
//  loveBee
//
//  Created by zhangxing on 2017/8/13.
//  Copyright © 2017年 zhangXing. All rights reserved.
//

import UIKit

class ZXHeaderCollectionReusableView: UICollectionReusableView {
    
    //实例化一个viewModel,(用来加载网络数据的对象)
    var homeViewModel = ZXHomeViewModel()
    
    
    /*
     1.     懒加载无限轮播视图
     2.     懒加载优惠视图
     3.     懒加载图片按钮视图
     
     
     */
    //1.     懒加载无限轮播视图(这一步执行了只是搭建了轮播的UI,至于开启定时器,只有在获取到网络数据并传递给轮播时才会开启)
    var cycleScrollView: CYImagePlayView = CYImagePlayView.init(frame: CGRect.zero, andActivity_mobileArray: nil)
    
    //2.     懒加载优惠视图icon
    var discountView: ZXIconDiscountView = ZXIconDiscountView()
    
    //3.     懒加载图片按钮视图btnImage
    var btnImageView: ZXBtnImageView = ZXBtnImageView()
    
    //4.     懒加载新鲜热卖label
    private lazy var hostSellLabel: UILabel = {
        
        let hostSellLabel = UILabel.init(fontSize: 14, textColor: UIColor.darkGray)
        
        hostSellLabel.text = "新鲜热卖"
        
        return hostSellLabel
    }()
    
    
    //自定义组头首先需要调用父类的super
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //加载网络数据
        homeViewModel.loadData { [unowned self] (isSucceed) in
            if isSucceed {
                
//                print("成功")
                
                //创建可变数组
                var urlStrList = [String]()
                
                //遍历模型数组获取url,添加到数组
                for cycleScrollModel in self.homeViewModel.cycleScrollModelList! {
                    
                    let urlStr = cycleScrollModel.img
                    
                    urlStrList.append(urlStr!)
                }
                
                //设置无限轮播图片(此时,无限轮播器接收到网络数据,才会开启定时器)
                self.cycleScrollView.imageArray = urlStrList
                
                //设置优惠icon图片
                self.discountView.iconModelList = self.homeViewModel.iconModelList
                
                
                //设置活动activity图片
                self.btnImageView.activityModelList = self.homeViewModel.activityModelList
                
            }
            else {
                
                print("失败")
            }
        }
        
        
        
        
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        //添加轮播图
        addSubview(cycleScrollView)
        //添加icon图
        addSubview(discountView)
        //添加activity图
        addSubview(btnImageView)
        
        //添加新鲜热卖label
        addSubview(hostSellLabel)
        
        
        btnImageView.backgroundColor = UIColor.init(hex: 0xEFEFEF)
        //添加轮播图约束
        cycleScrollView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            
            make.width.equalTo(KWidth)
            
            make.height.equalTo(150)
            
            make.centerX.equalToSuperview()
        }
        
        //添加icon图约束
        discountView.snp.makeConstraints { (make) in
            make.top.equalTo(cycleScrollView.snp.bottom)
            
            make.centerX.width.equalToSuperview()
            
            make.height.equalTo(80)
        }
        
        //添加activity图约束
        btnImageView.snp.makeConstraints { (make) in
            make.top.equalTo(discountView.snp.bottom)
            
            make.centerX.width.equalToSuperview()
            //留下来的30的高度设置成新鲜热卖labelView
            make.bottom.equalToSuperview().offset(-30)
        }
        
        //添加新鲜热卖label的约束
        hostSellLabel.snp.makeConstraints { (make) in
            make.top.equalTo(btnImageView.snp.bottom)
            
            make.leading.equalToSuperview().offset(10)
            
            make.bottom.centerX.equalToSuperview()
        
        }
        
        
        
        
    }
}
